package huoye.shops.huoye.operation.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.method.ScrollingMovementMethod;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.codeideology.android.utils.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.R;
import huoye.shops.base.APIUrls;
import huoye.shops.base.BaseActivity;
import huoye.shops.huoye.operation.adapter.SharePondAdapter;
import huoye.shops.huoye.operation.model.EventInfo;
import huoye.shops.huoye.operation.model.EventOddPrizeList;
import huoye.shops.huoye.operation.model.GuessAll;
import huoye.shops.huoye.operation.model.ManageEventData;
import huoye.shops.huoye.operation.model.Prize;
import huoye.shops.huoye.operation.model.SharePondData;
import huoye.shops.huoye.operation.model.SharePool;
import huoye.shops.huoye.operation.model.ToUrl;
import huoye.shops.huoye.operation.presenter.MineSetEventDetailsPresenter;
import huoye.shops.huoye.operation.presenter.SharePondPresenter;
import huoye.shops.huoye.operation.view.IMineSetEventDetailsView;
import huoye.shops.huoye.operation.view.ISharePondView;
import huoye.shops.huoye.operation.widget.EventCodeDialog;
import huoye.shops.utils.StockUtils;
import huoye.shops.utils.ViewUtils;
import huoye.shops.widget.CommonHintDialog;
import huoye.shops.widget.RoundImageView;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static huoye.shops.huoye.operation.activity.ManagePrizeActivity.EXTRAS_INTENT_PRIZE_TYPE_URL;
import static huoye.shops.huoye.operation.activity.ManagePrizeActivity.EXTRAS_KEY_PRIZE_SELECT_IMAGE;
import static huoye.shops.huoye.operation.activity.ManagePrizeActivity.EXTRAS_KEY_PRIZE_SELECT_PRIZE_ID;
import static huoye.shops.huoye.operation.activity.ManagePrizeActivity.EXTRAS_KEY_PRIZE_SELECT_STOCK;
import static huoye.shops.huoye.operation.activity.ManagePrizeActivity.EXTRAS_KEY_PRIZE_TYPE;
import static huoye.shops.utils.StockUtils.setTimeStyle;

public class MineSetEventDetailsActivity extends BaseActivity implements IMineSetEventDetailsView {

    @BindView(R.id.mine_set_event_details_tv_rule)
    TextView tvRule;

    @BindView(R.id.mine_set_event_details_tv_lottery_num)
    TextView tvLotteryNum;


    @BindView(R.id.mine_set_event_details_tv_residue_time_day)
    TextView tvTimeOne;
    @BindView(R.id.mine_set_event_details_tv_residue_time_hout)
    TextView tvTimeTwo;
    @BindView(R.id.mine_set_event_details_tv_residue_time_minute)
    TextView tvTimeThree;

    @BindView(R.id.mine_set_event_details_rv)
    RecyclerView rv;

    private SharePondAdapter adapter;

    private MineSetEventDetailsPresenter mMineSetEventDetailsPresenter;


    @BindView(R.id.mine_set_event_details_btn_cancel_event)
    Button btnCancelEvent;

    @OnClick(R.id.mine_set_event_details_btn_cancel_event)
    void onCancelEventClick() {
        //删除活动
        showDeleteHint();

    }

    private void showDeleteHint() {
        CommonHintDialog commonHintDialog = new CommonHintDialog(this);
        commonHintDialog.setOnCommonHintListener(new CommonHintDialog.OnCommonHintListener() {
            @Override
            public void onLeftClick() {

            }

            @Override
            public void onRightClick() {
                //自己的活动可以删除
                mMineSetEventDetailsPresenter.cancelEvent(mEventData.getEventId());
            }
        });
        commonHintDialog.show();
        commonHintDialog.setDialogContent("确定取消活动？", "取消", "确定");

    }


    private Date eDate;
    private ManageEventData mEventData;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_mine_set_event_details;
    }

    @Override
    protected void initView(@Nullable Bundle bundle) {
        setTitle("自建活动");
        mEventData = getIntentData();
        mMineSetEventDetailsPresenter = new MineSetEventDetailsPresenter();
        mMineSetEventDetailsPresenter.addTaskListener(this);
        mMineSetEventDetailsPresenter.onStart();
        mMineSetEventDetailsPresenter.getEventDetail(mEventData.getEventId());

    }


    @Override
    public void onTopRightIvClick() {
        super.onTopRightIvClick();
        mMineSetEventDetailsPresenter.eventToUrl(mEventData.getEventId());
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(mRunnable);
    }

    private Handler handler = new Handler();
    private Runnable mRunnable;

    private void timer() {
        mRunnable = new Runnable() {
            @Override
            public void run() {
                if (eDate != null) {
                    showDistanceTime(eDate);
                }
                handler.postDelayed(this, 1000);
            }
        };
        handler.postDelayed(mRunnable, 1000);
    }


    private void showDistanceTime(Date endDate) {
        Date now = new Date();

        long hour = ViewUtils.getDistanceTimeHourDay(now, endDate);
        long min = ViewUtils.getDistanceTimemin(now, endDate);
        long second = ViewUtils.getDistanceTimeSecond(now, endDate);

        if (hour == 0 && min == 0 && second == 0) {
            makeToast("活动结束");
            hideRightBtn();
            btnCancelEvent.setVisibility(GONE);
            handler.removeCallbacks(mRunnable);
            tvTimeOne.setText("00");
            tvTimeTwo.setText("00");
            tvTimeThree.setText("00");
            eDate = null;
        }

        if (hour == 0 && min == 0) {
            tvTimeOne.setText("00");
            tvTimeTwo.setText("00");
            tvTimeThree.setText(setTimeStyle(second));
        } else if (hour == 0) {
            tvTimeOne.setText("00");
            tvTimeTwo.setText(setTimeStyle(min));
            tvTimeThree.setText(setTimeStyle(second));
        } else {
            tvTimeOne.setText(setTimeStyle(hour));
            tvTimeTwo.setText(setTimeStyle(min));
            tvTimeThree.setText(setTimeStyle(second));
        }


    }
    private List<EventOddPrizeList> eventOddPrizeList;
    private String EventType = "1";
    private SharePondData mSharePondData;
    @Override
    public void setEventData(SharePondData sharePondData) {
        this.mSharePondData = sharePondData;
        EventInfo eventInfo = sharePondData.getEventInfo();
        setRightTv("活动二维码");
        showRightBtn();
        tvLotteryNum.setText(sharePondData.getSharePool().getUserLotteryNum());
        String endTime = eventInfo.getEndTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            eDate = sdf.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //活动状态 0-未开始 1-已开始 2-已结束
        if (sharePondData.getEventInfo().getiState() == 2) {


            tvTimeOne.setText("00");
            tvTimeTwo.setText("00");
            tvTimeThree.setText("00");
            btnCancelEvent.setVisibility(GONE);
            hideRightBtn();
        } else {
            timer();
        }


        if (eventInfo.getRemarks() == null) {
            eventInfo.setRemarks("");
        }
        tvRule.setText(eventInfo.getRemarks());
        tvRule.setMovementMethod(ScrollingMovementMethod.getInstance());
        tvRule.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    //通知父控件不要干扰
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                }
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    //通知父控件不要干扰
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                }
                return false;
            }
        });

        showRvSharePond();
    }



    private void showRvSharePond() {
        SharePool sharePool = mSharePondData.getSharePool();
        eventOddPrizeList = sharePool.getEventOddPrizeList();
        adapter = new SharePondAdapter(this, EventType, true);
        adapter.setData(eventOddPrizeList);
        adapter.setOnSharePondListener(new SharePondAdapter.OnSharePondListener() {
            @Override
            public void onAddPrizeClick(EventOddPrizeList eventOddPrizeList, int i) {

            }

            @Override
            public void onDelPrizeClick(EventOddPrizeList list, int i) {

            }

        });
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);
    }





    @Override
    public void getEventUrl(ToUrl toUrl) {
        EventCodeDialog eventCodeDialog = new EventCodeDialog(this);
        eventCodeDialog.show();
        eventCodeDialog.encode(toUrl.getUrl());
    }


}
